package java101;

import java.io.*;
import java.io.FileReader;
import java.io.IOException;

public class WorkingWithWords {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		String[] words = new String[10000];
		String [] counter = new String[10000];
		int progression = 0;
		
		//Read in text file
		FileReader fileReader = new FileReader("10kwords.txt");
		
		//Turn file into readable code
		BufferedReader bufferedReader = new BufferedReader(fileReader); 

		
		//Populates an array with the words
		for (int i=0; i<10000; i++) {
			
		String oneLine = bufferedReader.readLine();
		oneLine = oneLine.toUpperCase();
		words[i] = oneLine;
				  
		}
		
		for (int j = 0; j < words.length; j++) {
			
			boolean longest = isWordAlphabetized(words[j]);
			
			if (longest == true) {
				counter[progression] = words[j];
				progression++;
			}
		
		}
		
		int letterCount = 0;
		for (int c = 0; c < counter.length; c++) {
			if (counter[c] != null){
				if (counter[c].length() > letterCount) {
					letterCount = counter[c].length();
				}
			}
		}
		
		System.out.println("The longest words have " + letterCount + " letters.");
		System.out.println();
		System.out.println("The longest words are: ");
		
		for (int d = 0; d < counter.length; d++) {
			if ((counter[d] != null) && (letterCount == counter[d].length())) {
				System.out.println(counter[d]);
			}
		}

	}
	
	

		public static boolean isWordAlphabetized(String words) {
			
			String wordAsString = words.toString();
			for (int h = 1; h < wordAsString.length(); h++) {
				char first = wordAsString.charAt(h-1);
				int firstAsNumber = Character.getNumericValue(first);
				char second = wordAsString.charAt(h);
				int secondAsNumber = Character.getNumericValue(second);
				
				if (secondAsNumber < firstAsNumber) {
					return false;
				}
				
			}
			return true;
			

}

}